package com.health.ts.controller.sysmanage;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.health.ts.constant.ApiResult;
import com.health.ts.constant.FrontPage;
import com.health.ts.controller.BaseController;
import com.health.ts.entity.sysmanage.SysDict;
import com.health.ts.entity.sysmanage.SysRole;
import com.health.ts.entity.sysmanage.SysRoleUser;
import com.health.ts.entity.sysmanage.SysUser;
import com.health.ts.entity.sysmanage.UserResetPwdReqVo;
import com.health.ts.service.sysmanage.SysDictService;
import com.health.ts.service.sysmanage.SysRoleService;
import com.health.ts.service.sysmanage.SysRoleUserService;
import com.health.ts.service.sysmanage.SysUserService;
import com.health.ts.utils.SessionUserUtil;


/**
 * 用户管理
 * @author Administrator
 */
@RestController
@RequestMapping(value = "/api/user",produces= BaseController.JSON_UTF8)
public class UserController extends BaseController {
	
	@Autowired
	SysUserService sysUserService;
	
	@Autowired
	SysRoleUserService  sysRoleUserService;
	
	@Autowired
	SysRoleService sysRoleService;
	
	@Autowired
	SysDictService sysDictService;
	
	/**
	 * 分页查询用户数据
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/usersPager",method=RequestMethod.GET)
	public ApiResult getUserListWithPager(FrontPage page) {
		return ApiResult.success(sysUserService.findUserPageList(page));
	}
	
	/**
	 * 查询用户列表(不带分页)
	 * @return
	 */
	@RequestMapping(value = "/users",method=RequestMethod.GET)
	public ApiResult listUsers(String hospital_id,String department_id) {
		return ApiResult.success(sysUserService.findUserList(hospital_id, department_id));
	}
	
	/**
	 * 获取单个用户
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/{userid}", method=RequestMethod.GET)
	public ApiResult selectUser(@PathVariable String userid){
		return ApiResult.success(sysUserService.findUserInfo(userid));
	}
	
	/**
	 * 根据user创建用户
	 * @param id
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="", method=RequestMethod.POST)
	public ApiResult addUser(@RequestBody SysUser sysUser){
		sysUserService.addUser(sysUser);
		return ApiResult.success();
	}
	
	/**
	 * 用户信息修改
	 * @param hospital
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/{userid}", method=RequestMethod.PUT)
	public ApiResult updateUser(@PathVariable String userid,@RequestBody SysUser sysUser){
		sysUserService.updateUser(userid, sysUser);
		return ApiResult.success();
	}
	
	/**
	 * 删除用户
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/{userid}", method=RequestMethod.DELETE)
	public ApiResult deleteUser(@PathVariable String userid) {
		sysUserService.removeById(userid);
		return ApiResult.success();
	}
	
	/**
	 * 用户重置密码
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/resetPwd", method=RequestMethod.POST)
	public ApiResult resetPwd(@RequestBody @Validated UserResetPwdReqVo reqVo) {
		sysUserService.resetUserPwd(reqVo);
		return ApiResult.success();
	}
	
	/**
	 * 获取登录用户信息
	 * @return
	 */
	@GetMapping(value="/userInfo")
	public ApiResult userInfo(){
		 SysUser sessionUser = SessionUserUtil.getUser();
		 sessionUser.setPasswd(null);
		 return ApiResult.success(sessionUser); 
	}
	
	/**
	 * 查询登录用户菜单
	 * @return
	 */
	@GetMapping(value = "/userMenus")
	public ApiResult listUserMenus() {
        SysUser sessionUser = SessionUserUtil.getUser();
    	return ApiResult.success(sysUserService.selectMenusByUser(sessionUser));
	}
	
	/**
	 * 查询用户类型列表
	 * @return
	 */
	@RequestMapping(value = "/userTypes",method=RequestMethod.GET)
	public ApiResult listUserTypes() {
		QueryWrapper<SysDict> QueryWrapper = new QueryWrapper<SysDict>();
		QueryWrapper.eq("group_code", "userTypeInfo").eq("valid_status", "1");
		QueryWrapper.orderByAsc("item_order");
		List<SysDict> list=sysDictService.list(QueryWrapper);
		return ApiResult.success(filteUserType(list));
	}
	
	/**
	 * 用户角色关联
	 * @param roleUser
	 * @return
	 */
	@RequestMapping(value="/addUserRole", method=RequestMethod.POST)
	public ApiResult addUserRole(@RequestBody SysRoleUser roleUser) {
		sysRoleUserService.saveOrUpdate(roleUser);
		return ApiResult.success();
    }
	
	/**
	 * 删除用户角色关联
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/userRole/{id}", method=RequestMethod.DELETE)
	public ApiResult deleteUserRole(@PathVariable String id) {
		sysRoleUserService.removeById(id);
		return ApiResult.success();
    }
	
	/**
	 * 查询用户角色关联列表
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/listUserRole", method=RequestMethod.POST)
	public ApiResult listUserRole(@PathVariable String id) {
		SysRoleUser roleUser=new SysRoleUser();
		roleUser.setUser_id(id);
		SysRole role=sysRoleUserService.selectRoles(roleUser);
		return ApiResult.success(role);
	}
	
	/**
	 * 过滤用户类型
	 * @param list
	 * @return
	 */
	private List<SysDict> filteUserType(List<SysDict> list){
		 //过滤掉超级管理员
        CollectionUtils.filter(list, new Predicate() {
            @Override
            public boolean evaluate(Object object) {
            	SysDict sysDict=(SysDict)object;
            	if(!"0".equals(sysDict.getItem_code())){
            		return true;
            	}
            	return false;
            }
        });
        
        //系统管理员过滤掉系统管理员类型
        CollectionUtils.filter(list, new Predicate() {
            @Override
            public boolean evaluate(Object object) {
            	SysDict sysDict=(SysDict)object;
        		if(SessionUserUtil.userIsSysAdmin()){
                	if("1".equals(sysDict.getItem_code())){
                		return false;
                	}
        		}
            	return true;
            }
        });
        
        //医院管理员过滤掉医院管理员类型
        CollectionUtils.filter(list, new Predicate() {
            @Override
            public boolean evaluate(Object object) {
            	SysDict sysDict=(SysDict)object;
        		if(SessionUserUtil.userIsHosAdmin()){
                	if("1".equals(sysDict.getItem_code())||"2".equals(sysDict.getItem_code())){
                		return false;
                	}
        		}
            	return true;
            }
        });
        
        return list;
	}
}
